package com.huarun.module.user.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.huarun.common.BaseController;
import com.huarun.common.ResponseMessage;
import com.huarun.common.util.HttpRequestUtils;
import com.huarun.common.util.PropertyUtil;
import com.huarun.common.util.UrlLib;
import com.huarun.module.sys.service.ISubjectService;
import com.huarun.module.user.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/login")
@Api(description = "用户")
public class GeneralTenantsController extends BaseController {
	@Resource
	private IUserService userService;
	@Resource
	private ISubjectService subjectService;

	@ApiOperation(value = "根据openId邀约人信息", notes = "春笋访客信息")
	@RequestMapping(value = "/invitedinfo", method = RequestMethod.POST)
	@ResponseBody
	public static ResponseMessage<Object> invitedinfo(HttpServletRequest request) {
		Map<String, Object> userParam = new HashMap<String, Object>();
		// userParam.put("openId", request.getHeader("openId"));
		// 根据openId获取用户信息
		// User user = userService.getUserByParams(userParam);
		// String wxPhone = user.getWxPhone();
		JSONObject jsonParam = new JSONObject();
		long newTime = new Date().getTime() / 1000;
		jsonParam.put("appkey", "2EGPTUR280SH5L73H594JUO519668J1V");
		jsonParam.put("timeDate", newTime);
		// 获取token
		JSONObject httpPost = HttpRequestUtils.httpPost("http://132.232.41.235:9998/Api/create/Sign", jsonParam);
		JSONObject jsonEmployee = new JSONObject();
		String token = httpPost.getJSONObject("Retdata").getString("token");
		jsonEmployee.put("token", token+"123");
		jsonEmployee.put("appkey", "2EGPTUR280SH5L73H594JUO5196681V222");
		// 18575576896
		jsonEmployee.put("mobile", "18575576896");
		// 根据电话号码获取用户信息
		JSONObject Employee = HttpRequestUtils.httpPost("http://132.232.41.235:9998/api/getEmployee", jsonEmployee);
		System.out.println(Employee);
		Employee.put("isDegree", 0);
		if (null != Employee) {
			Map<String, Object> subMap = Employee;
			System.out.println(subMap.get("errcode"));
			Map<String, Object> employeMap = (Map<String, Object>) subMap.get("Retdata");
			if ((Integer)(employeMap.get("flag"))==1 ) {
				System.out.println(employeMap.get("companyName"));
				System.out.println(employeMap.get("empName"));
			} else {
				System.out.println("未查询到用户信息");
			}
		}
		ResponseMessage<Object> rm = new ResponseMessage<Object>();
		rm.setData(Employee);
		return rm;
	}

	@ApiOperation(value = "获取face++信息", notes = "登录获取face++信息")
	@RequestMapping(value = "/loginFacePlus", method = RequestMethod.GET)
	@ResponseBody
	public static String loginFacePlus(HttpServletRequest request) {
		String uri = PropertyUtil.getProperty("face_url_B");
		String url = uri + PropertyUtil.getProperty("appLogin");
		Map<String, String> params = new HashMap<>();
		params.put("username", "hrzd@megvii.com");
		params.put("password", "123456");
		try {
			byte[] resultJson = UrlLib.doPost(request, url, params);

			return new String(resultJson, "UTF-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		invitedinfo(null);
	}

}
